<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 用el-tree布局主体内容 -->
        <el-tree :data="list" default-expand-all>
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children&&data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 实现新增子部门或者编辑部门的对话框 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '新增部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="resetForm"
      >
        <!-- 在弹层中放入子组件 -->
        <!-- 解决数据不更新的问题:方法一:v-if=showDialog -->
        <!-- 方法二 ref=addOrEdit -->
        <addOrEdit ref="addOrEdit" :is-edit="isEdit" :pid="pid" :origin-list="originList" @success="success" @close="showDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranDataToTree } from '@/utils/index'
import addOrEdit from './deptDialog.vue'
export default {
  components: { addOrEdit },
  data() {
    return {
      // 保存对应的pid
      pid: '',
      // 控制对话框的显示和隐藏
      showDialog: false,
      // 表单是编辑还是新增功能
      isEdit: false,
      // 依赖一份树形数据
      list: [],
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 重置子组件中的表单
    resetForm() {
      // 在父组件中调用子组件中的重置方法
      this.$refs.addOrEdit.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { data: res } = await delDepartments(id)
        console.log('hDel', res)
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑功能需要变量传递状态
    hEdit(id) {
      this.isEdit = true
      this.pid = id
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.addOrEdit.loadDepartmentsDetails()
      })
    },
    // 新增/修改成功 : 关闭对话框,重新渲染页面
    success() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 新增子部门需要获取父级pid
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      // 获取对应的id
      this.pid = id
    },
    // 获取员工列表数据
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        console.log('getDepartments', res.data)
        // 优化,只获取数组中每个对象的部门数据(id,pid,name,code)用于列表渲染
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // 删除数组中一个数据,去除公司的名称
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
        // console.log('res.data.depts', res.data.depts)
        console.log('tranDataToTree', this.list)
      } catch (error) {
        console.log('getDepartments发送请求错误', error)
      }
    }
  }
}
</script>
